package com.lc.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.reggie.dao.DishMapper;
import com.lc.reggie.dto.DishDto;
import com.lc.reggie.entity.Dish;
import com.lc.reggie.entity.DishFlavor;
import com.lc.reggie.entity.SetmealDish;
import com.lc.reggie.service.DishFlavorService;
import com.lc.reggie.service.DishService;
import com.lc.reggie.service.SetmealDishService;
import com.lc.reggie.common.CustomException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService
{
	
	@Autowired
	private DishFlavorService dishFlavorService;
	
	@Autowired
	private SetmealDishService setmealDishService;
	
	/**
	 * 新增菜品，同时保存对应的口味数据
	 * @param dishDto
	 */
	@Override
	@Transactional
	public void saveWithFlavor(DishDto dishDto)
	{
		//保存菜品的基本信息到菜品表dish
		this.save(dishDto);
		
		Long dishId = dishDto.getId();//菜品id
		
		//菜品口味
		List<DishFlavor> flavors = dishDto.getFlavors();
		flavors = flavors.stream().map((item) -> {
			item.setDishId(dishId);
			return item;
		}).collect(Collectors.toList());
		
		//保存菜品口味数据到菜品口味表dish_flavor
		dishFlavorService.saveBatch(flavors);
		
	}
	
	/**
	 * //根据id查询菜品信息和对应的口味信息
	 * @param id
	 * @return
	 */
	@Override
	public DishDto getByIdWithFlavor(Long id)
	{
		//新建一个dishDtao来封装菜品信息和口味信息
		DishDto dishDto = new DishDto();
		
		//查询菜品基本信息，从dish表查询
		Dish dish = this.getById(id);
		//拷贝
		BeanUtils.copyProperties(dish, dishDto);
		
		//查询当前菜品对应的口味信息，从dish_flavor表查询
		LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
		lqw.eq(DishFlavor::getDishId, dish.getId());
		List<DishFlavor> flavors = dishFlavorService.list(lqw);
		
		dishDto.setFlavors(flavors);
		
		return dishDto;
	}
	
	/**
	 * //更新菜品信息，同时更新对应的口味信息
	 * @param dishDto
	 */
	@Override
	@Transactional
	public void updateWithFlavor(DishDto dishDto)
	{
		//更新dish表基本信息
		this.updateById(dishDto);
		
		//清理当前菜品对应口味数据---dish_flavor表的delete操作
		Long dishId = dishDto.getId();
		LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
		lqw.eq(DishFlavor::getDishId, dishId);
		dishFlavorService.remove(lqw);
		
		//添加当前提交过来的口味数据---dish_flavor表的insert操作
		List<DishFlavor> flavors = dishDto.getFlavors();
		flavors = flavors.stream().map((item) -> {
			item.setDishId(dishId);
			return item;
		}).collect(Collectors.toList());
		
		dishFlavorService.saveBatch(flavors);
	}
	
	/**
	 * 删除菜品，同时删除菜品和口味的关联关系，还有套餐菜品关系
	 * @param ids
	 */
	@Override
	public void deleteWithFlavor(List<Long> ids)
	{
		//查询菜品状态，确定是否可用删除(关于是否正在售卖)
		LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper<>();
		lqw1.in(Dish::getId, ids);
		lqw1.eq(Dish::getStatus, 1);
		int count1 = this.count(lqw1);
		if (count1 > 0)
		{
			//如果不能删除，抛出一个业务异常
			throw new CustomException("菜品正在售卖中，不能删除");
		}
		
		//查询菜品状态，确定是否可用删除(关于是否在套餐内)
		LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
		lqw2.in(SetmealDish::getDishId, ids);
		int count2 = setmealDishService.count(lqw2);
		if (count2 > 0)
		{
			//如果不能删除，抛出一个业务异常
			throw new CustomException("菜品正在套餐中，不能删除");
		}
		
		
		//先删除菜品口味表中的数据---dish_flavor
		LambdaQueryWrapper<DishFlavor> lqw3 = new LambdaQueryWrapper<>();
		lqw3.in(DishFlavor::getDishId, ids);
		dishFlavorService.remove(lqw3);
		
		//再删除菜品表中的数据---dish
		this.removeByIds(ids);
		
		//对嘛，菜品不在套餐里面，怎么会要处理套餐菜品表嘞
//		//删除关系表中的数据----setmeal_dish
//		LambdaQueryWrapper<SetmealDish> lqw4 = new LambdaQueryWrapper<>();
//		lqw4.in(SetmealDish::getDishId, ids);
//		setmealDishService.remove(lqw4);
	
	}
	
	
//	@Autowired
//	private DishMapper dishMapper;
	/**
	 * 停售或起售菜品
	 * @param status
	 * @param ids
	 */
	@Override
	@Transactional
	public void stopOrStartSell(int status, List<Long> ids)
	{
		//查询批量菜品
		List<Dish> dishes = this.listByIds(ids);
//		//删除批量菜品
//		this.removeByIds(ids);
//		dishes = dishes.stream().map((item) -> {
//			item.setStatus(status);
//			return item;
//		}).collect(Collectors.toList());
//		//新增批量菜品
//		this.saveBatch(dishes);
		for (Dish dish : dishes)
		{
			dish.setStatus(status);
			this.updateById(dish);
		}
//		return dishMapper.update(status, ids) > 0;
	}
	
}
